package com.lab.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 工具类
 * 
 * @author WuHao
 * @since Jul 28, 2011 2:55:33 PM
 * @version 2011
 */

public class UtilTools {
	/**
	 * int转16进制字符
	 * 
	 * @param num
	 * @return String 16进制
	 */
	public static String mkHexStr(int num) {

		final char digits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
				'9', 'A', 'B', 'C', 'D', 'E', 'F' };

		int length = 32;
		char[] result = new char[length];

		do {
			result[--length] = digits[num & 15];
			num >>>= 4;
		} while (num != 0);
		String res = "";
		for (int i = length; i < result.length; i++) {
			res += result[i];
		}
		return res;
	}

	/**
	 * String不足两位补0的方法
	 * 
	 * @param str
	 *            传入的String
	 * @param strLength
	 *            处理之后的总长度
	 * @return 补0成功的String
	 */
	public static String addZeroForNum(String str, int strLength) {
		int strLen = str.length();
		if (strLen < strLength) {
			while (strLen < strLength) {
				StringBuffer sb = new StringBuffer();
				sb.append("0").append(str);// 左补0
				// sb.append(str).append("0");//右补0
				str = sb.toString();
				strLen = str.length();
			}
		}
		return str;
	}

	/**
	 * MD5不可逆加密算法
	 * 
	 * @param String
	 *            需要加密的字符串
	 * @return String 加密后的字符串
	 */
	public static String MD5(String ori) {
		if (null == ori)
			return "";
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(ori.getBytes());
			byte b[] = md.digest();
			int i;
			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			return buf.toString().toUpperCase();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 4位二进制转换成16进制字符
	 */
	public static String mk4TOHex(String code) {
		String result = "";
		switch (Integer.parseInt(code)) {
		case 0:
			result = "0";
			break;
		case 1:
			result = "1";
			break;
		case 10:
			result = "2";
			break;
		case 11:
			result = "3";
			break;
		case 100:
			result = "4";
			break;
		case 101:
			result = "5";
			break;
		case 110:
			result = "6";
			break;
		case 111:
			result = "7";
			break;
		case 1000:
			result = "8";
			break;
		case 1001:
			result = "9";
			break;
		case 1010:
			result = "A";
			break;
		case 1011:
			result = "B";
			break;
		case 1100:
			result = "C";
			break;
		case 1101:
			result = "D";
			break;
		case 1110:
			result = "E";
			break;
		case 1111:
			result = "F";
			break;
		default:
			result = "0";
			break;
		}
		return result;
	}
	
	
	
	

	public static void main(String[] args) {
		System.out.println("admin==" + UtilTools.MD5("admin"));
		System.out.println("wuhao==" + UtilTools.MD5("wuhao"));
		System.out.println("chaibq=" + UtilTools.MD5("chaibq"));
		System.out.println(Math.max(1, 1.1));
	}

	/**
	 * RS816特殊处理字符串
	 * 
	 * @param inputValue
	 * @return
	 */
	public static String inputValueDealForRS816(String result) {
		StringBuffer sb = new StringBuffer();
		String[] stringArray = result.split("!");
		for (int i = 1; i < stringArray.length; i++) {
			sb = sb.append(stringArray[i]);
		}
		final int dight = 2;
		int sumNumber = (int) toInt_b(sb.toString(), dight);
		int forNotNumber = 0xFFFF;
		int resultNumber = forNotNumber - sumNumber;
		String result0x = Integer.toHexString(resultNumber);
		String result0xFinal = UtilTools.addZeroForNum(result0x, 2);
		String newStr = SocketMethod.addSpliter(result0xFinal);
		String finalStr = result + newStr;
		return finalStr;
	}

	/**
	 * 直接进行计算
	 * 
	 * @param hexStr
	 *            十六进制字符串
	 * @param dight
	 *            从右边开始进行拆分的位数
	 */
	public static long toInt_b(String hexStr, int digit) {
		if (digit <= 0) {
			throw new IllegalArgumentException(
					"argument digit must be greater than zero!");
		}
		char[] chs = hexStr.toCharArray();
		long sum = 0;
		for (int i = chs.length - 1; i >= 0; i--) {
			sum += Character.digit(chs[i], 16)
					* (1L << ((chs.length - i - 1) % digit * 4));
		}
		return sum;
	}

	/**
	 * Start
	 * Rz_6000开关量进制处理
	 */
//	private static String[] hexs = new String[] { "0", "1", "2", "3", "4", "5",
//			"6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
//	private static String[] bins = new String[] { "0000", "0001", "0010",
//			"0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010",
//			"1011", "1100", "1101", "1110", "1111" };
//	
//	// 将十进制数hex转换为二进制数并返回
//	public static String convertHexToBin(String hex) {
//		StringBuffer buff = new StringBuffer();
//		int i;
//		for (i = 0; i < hex.length(); i++) {
//			buff.append(getBin(hex.substring(i, i + 1)));
//		}
//		return buff.toString();
//	}
//	
//	// 返回十六进制数的二进制形式
//	private static String getBin(String hex) {
//		int i;
//		for (i = 0; i < hexs.length && !hex.toLowerCase().equals(hexs[i]); i++);
//		return bins[i];
//	}
	public static int getIndexOfByte(byte b,int index){
		int train = (int)Math.pow(2, (index-1));
		int rs = b&train;
		return (rs==0?0:1);
	}
	/**
	 * end
	 */
}
